﻿
using Model.Core;
using Models;
using SCADA_DAQ.ZEMT.Server;
using SIE.SCADA.Common.Log;
using SIE.SCADA.SqlSugar;
using SIE.SCADA.ZemtDemo.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIE.SCADA.ZemtDemo.ViewModel
{
    public class MatProductRelationViewModel : QueryBaseViewModel<scada_MatProductRelation>
    {
        PgSqlAccess pgsql = new PgSqlAccess();
        public string _productid;
        public string productid
        {
            get { return _productid; }
            set
            {
                if (value != _productid)
                {
                    _productid = value;
                    OnPropertyChanged(nameof(productid));
                }
            }
        }

        public string _matid;
        public string matid
        {
            get { return _matid; }
            set
            {
                if (value != _matid)
                {
                    _matid = value;
                    OnPropertyChanged(nameof(matid));
                }
            }
        }



        public MatProductRelationViewModel()
        {
            
        }

        public override void Query()
        {
            using (var ctx = pgsql.GetClient())
            {
                Records = new List<scada_MatProductRelation>();
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {

                    string sql = " 1=1 ";
                    if (matid !=null && matid != "")
                    {
                        sql += " and  matid like  " + "'%" + matid + "%'";
                    }
                    if (productid != null && productid != "")
                    {
                        sql += " and  productid like  " + "'%" + productid + "%'";
                    }
                    

                    Records = ormClient.SqlSugarClient.Queryable<scada_MatProductRelation>().Where(sql).ToList();



                    
                }
            }
        }



        //public  void Query(string WorkID)
        //{
        //    using (var ctx = pgsql.GetClient())
        //    {
        //        Records = new List<scada_MatProductRelation>();
        //        if (pgsql.DBClient is SmdcOrmClient ormClient)
        //        {
        //            if (String.IsNullOrEmpty(WorkID))
        //            {
        //                Records = ormClient.SqlSugarClient.Queryable<scada_MatProductRelation>().ToList();
        //            }
        //            else {
        //                Records = ormClient.SqlSugarClient.Queryable<scada_MatProductRelation>().Where(m => m.workhour_num==WorkID).ToList();
        //            }
                    
        //        }
        //    }
        //}





        public override void Delete()
        {
            if (CurItem != null)
            {
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        ormClient.SqlSugarClient.Deleteable<scada_MatProductRelation>().Where(m => m.id == CurItem.id).ExecuteCommand();
                    }
                }
                base.Delete();
            }
        }

        public override void Update()
        {
            if (CurItem != null)
            {
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        ormClient.SqlSugarClient.Updateable(CurItem).Where(m => m.id == CurItem.id).ExecuteCommand();
                    }
                }
                base.Update();
            }
            //if(string.IsNullOrEmpty(CurItem.equipmentid))
            //{
            //    equipManager.Request(CurItem.equipmentid);
            //}
        }

        internal void GetWorkInfo(string WorkInfoID)
        {
            

        }






        public override void Add()
        {
            using (var ctx = pgsql.GetClient())
            {
                try
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        var process = new scada_MatProductRelation();
                        ormClient.SqlSugarClient.Insertable<scada_MatProductRelation>(process).IgnoreColumns(it => it == "id").ExecuteCommand();
                        Query();
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex.Message);
                    Logger.Error(ex.StackTrace);
                }
                base.Add();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateFromMq()
        {

        }

    }
}
